package com.dd.census.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.dd.census.entity.SysMenu;
import com.dd.census.mapper.SysMenuMapper;
import com.dd.census.service.SysMenuService;

@Service
public class SysMenuServiceImpl implements SysMenuService {

	@Autowired
	private SysMenuMapper sysMenuMapper;

	public Map<String, Object> selectPage(SysMenu sysMenu) {
		Map<String, Object> map = new HashMap<String, Object>();
		int total = sysMenuMapper.selectCount(sysMenu);
		map.put("pageCount", Math.ceil((double) total / sysMenu.getPageSize()));// 总页数
		map.put("total", total);// 总条数
		map.put("index", sysMenu.getPageIndex());// 当前页
		if (total > 0) {
			sysMenu.setPageIndex(sysMenu.getPageSize() * (sysMenu.getPageIndex() - 1));
			map.put("items", sysMenuMapper.selectPage(sysMenu));
		}
		return map;
	}

	public SysMenu selectById(Integer id) {
		return sysMenuMapper.selectById(id);
	}

	public Map<String, Object> selectName(SysMenu sysMenu) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("items", sysMenuMapper.selectName(sysMenu));
		return map;
	}

	public Map<String, Object> selectChild(SysMenu sysMenu) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("items", sysMenuMapper.selectChild(sysMenu));
		return map;
	}

	public List<SysMenu> selectAll(Integer userId) {
		List<SysMenu> resultList = new ArrayList<>();
		List<SysMenu> entityList= sysMenuMapper.selectAll(userId);
	     //获取顶层元素集合
	     Integer parentCode;
	     for (SysMenu entity : entityList) {
	         parentCode = entity.getIsParent();
	         //顶层元素的parentCode==null或者为0
	         if (parentCode == null || 1 == parentCode) {
                 resultList.add(entity);
             }
         }
	     //获取每个顶层元素的子数据集合
	     for (SysMenu entity : resultList) {
	    	 List<SysMenu> subList = getSubList(entity.getId(), entityList);
	    	 entity.setChildMenus(subList ==null ? new ArrayList<SysMenu>() :subList);
	     }
		return resultList;
	}

	@Transactional
	public int insert(SysMenu sysMenu) {
		sysMenuMapper.insert(sysMenu);
		// 当为父级菜单时对父id进行赋值
		if (sysMenu.getParentId() == null) {
			SysMenu sysMenus = new SysMenu();
			sysMenus.setId(sysMenu.getId());
			sysMenus.setVersion(0);
			sysMenus.setParentId(sysMenu.getId());
			sysMenuMapper.update(sysMenus);
		}
		return sysMenu.getId();
	}

	@Transactional
	public int update(SysMenu sysMenu) {
		return sysMenuMapper.update(sysMenu);
	}
	
	@Transactional
	public int delete(SysMenu sysMenu) {
		// 删除当前菜单
		sysMenuMapper.delete(sysMenu);
		// 删除子菜单
		sysMenuMapper.deleteByParentId(sysMenu.getId());
		return sysMenuMapper.delete(sysMenu);
	}

	@Override
	public Map<String, Object> selectList(SysMenu sysMenu) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("items", sysMenuMapper.selectList(sysMenu));
		return map;
	}

	/**
	 * 获取子数据集合
	 * @param id
	 * @param entityList
	 * @return
	 */
    private static List<SysMenu> getSubList(Integer id, List<SysMenu> entityList) {
        List<SysMenu> childList = new ArrayList<SysMenu>();
        Integer parentId;
        //子集的直接子对象
        for (SysMenu entity : entityList) {
            parentId = entity.getParentId();
            if (id.equals(parentId)) {
            	childList.add(entity);
            }
        }
        //子集的间接子对象
        for (SysMenu entity : childList) {
            entity.setChildMenus(getSubList(entity.getId(), entityList));
        }
        //递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }
}